<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    
    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $devicelist = $loginlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $begin = strtotime($day . ' 00:00:00');
            $end = strtotime($day . ' 23:59:59');
            
            $temp = Db::name('UserLogin')->distinct(true)->field('uid')->whereBetween('createtime', [$begin, $end])->select();
            $loginlist[$day] = count($temp); 
            $devicelist[$day] = Db::name('device')->whereBetween('createtime', [$begin, $end])->count();
        }
        
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        
        $totalUser = model('User')->count();
        $totalDevice = model('Device')->count();
        $totalOrder = model('User')->where(['is_pay'=>1])->count();
        
        $this->view->assign([
            'totaluser'        => $totalUser,
            'totalviews'       => $totalDevice,
            'totalorder'       => $totalOrder,
            'totalorderamount' => 0,
            'todayuserlogin'   => 321,
            'todayusersignup'  => 430,
            'todayorder'       => 2324,
            'unsettleorder'    => 132,
            'sevendnu'         => '80%',
            'sevendau'         => '32%',
            'devicelist'       => $devicelist,
            'loginlist'        => $loginlist,
            'addonversion'     => $addonVersion,
            'uploadmode'       => $uploadmode
        ]);

        return $this->view->fetch();
    }
    
    /**
     * 获取信息
     */
    public function get(){
        $begin = $this->request->post('begin');
        $end = $this->request->post('end');
        
        $beginTime = strtotime($begin);
        $endTime = strtotime($end);
        
        $devicelist = $loginlist = $daylist = [];
        $days = \fast\Date::span($beginTime, $endTime, 'days'); // 获取时间差值
        for ($i = 0; $i <= $days; $i++) 
        {
            $day = date("Y-m-d", $beginTime + ($i * 86400));
            $b = strtotime($day . ' 00:00:00');
            $e = strtotime($day . ' 23:59:59');
            
            $temp = Db::name('UserLogin')->distinct(true)->field('uid')->whereBetween('createtime', [$b, $e])->select();
            $loginlist[] = count($temp);
            $devicelist[] = Db::name('device')->whereBetween('createtime', [$b, $e])->count();
            $daylist[] = $day;
        }
        
        return json(['devicelist'=>$devicelist, 'loginlist'=>$loginlist, 'daylist'=>$daylist]);
    }
    
    public function excel(){
        $begin = $this->request->post('begin');
        $end = $this->request->post('end');
        
        $beginTime = strtotime($begin);
        $endTime = strtotime($end);
        
        $devicelist = $loginlist = $daylist = [];
        $days = \fast\Date::span($beginTime, $endTime, 'days'); // 获取时间差值
        for ($i = 0; $i <= $days; $i++)
        {
            $day = date("Y-m-d", $beginTime + ($i * 86400));
            $b = strtotime($day . ' 00:00:00');
            $e = strtotime($day . ' 23:59:59');
            
            $temp = Db::name('UserLogin')->distinct(true)->field('uid')->whereBetween('createtime', [$b, $e])->select();
            $loginlist[$day] = count($temp);
            $devicelist[$day] = Db::name('device')->whereBetween('createtime', [$b, $e])->count();
            $daylist[] = $day;
        }
        
        ob_start();
        $excel = new \PHPExcel();
        $write = new \PHPExcel_Writer_Excel2007($excel);
        
        // 设置当前的sheet init info
        $excel->setActiveSheetIndex(0);
        $row = 1;
        $sheet = $excel->getActiveSheet();
        
        // sheet
        $sheet->setTitle("find data");
        // 设置当前表的宽度
        $sheet->getColumnDimension("A")->setWidth(20);
        $sheet->getColumnDimension("B")->setWidth(12);
        $sheet->getColumnDimension("C")->setWidth(12);
        
        // 设置第一行
        $sheet->getRowDimension($row)->setRowHeight(35);
        $sheet->setCellValue("B$row", '装机量');
        $sheet->setCellValue("C$row", '活跃用户');
        $row++;
        
        // 设置数据
        for ($i = 0; $i < count($daylist); $i++){
            $day = $daylist[$i];
            
            $sheet->setCellValue("A$row", $day);
            $sheet->setCellValue("B$row", $devicelist[$day]);
            $sheet->setCellValue("C$row", $loginlist[$day]);
            
            $row++;
        }
        
        $filename = 'export.xlsx';
        $write->save("php://output");
        $xlsData = ob_get_contents();
        ob_end_clean();
        
        return json(['filename' => $filename, 'file' => "data:application/vnd.ms-excel;base64,".base64_encode($xlsData)]);
    }

}
